package com.qf.sl.youshare;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import com.qf.sl.youshare.adapters.CartAdapter;
import com.qf.sl.youshare.model.GoodsCart;
import com.qf.sl.youshare.tasks.GoodsCartTask;
import com.qf.sl.youshare.tasks.TaskProcessor;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;

/**
 * Created by IDEA
 * User : sl
 * on 2015/2/25.
 * 10:48
 */
public class CartActivity extends Activity implements CompoundButton.OnCheckedChangeListener, View.OnClickListener, TaskProcessor {

    private TextView txtTotalFee;
    private Button btnCheckout;

    private CartAdapter adapter;
    private LinkedList<GoodsCart> goodsCarts;
    private CheckBox selectAll;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);

        ListView cartListView = (ListView) findViewById(R.id.cart_list);
        txtTotalFee = (TextView) findViewById(R.id.cart_total_fee);
        btnCheckout = (Button) findViewById(R.id.cart_checkout);
        selectAll = (CheckBox) findViewById(R.id.cart_selete_all);
        selectAll.setOnCheckedChangeListener(this);

        goodsCarts = new LinkedList<GoodsCart>();

        GoodsCartTask task = new GoodsCartTask(this);
        task.execute();

        adapter = new CartAdapter(this, goodsCarts);

        cartListView.setAdapter(adapter);

        adapter.setCheckedChangeListener(this);
        adapter.setOperationListener(this);

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        int id = buttonView.getId();
        switch (id){
            case R.id.item_cart_checked:
                Object tag = buttonView.getTag();
                if (tag != null) {
                    if (tag instanceof Integer){
                        int position = (Integer)tag;
                        GoodsCart goodsCart = goodsCarts.get(position);
                        goodsCart.setChecked(isChecked);
                        boolean selete_all = true;
                        for (GoodsCart cart : goodsCarts) {
                            boolean checked = cart.isChecked();
                            if (!checked){
                                selete_all = false;
                            }
                        }
                        selectAll.setChecked(selete_all);
                    }
                }
                break;
            case R.id.cart_selete_all:
                for (GoodsCart goodsCart : goodsCarts) {
                    goodsCart.setChecked(isChecked);
                }
                break;
        }
        adapter.notifyDataSetChanged();
        calcTotalFee();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        int position = 0;
        Object tag = v.getTag();
        if (tag != null) {
            if (tag instanceof Integer){
                position = (Integer)tag;
            }
        }
        GoodsCart goodsCart = goodsCarts.get(position);
        switch (id){
            case R.id.item_cart_delete:
                goodsCarts.remove(position);
                break;
            case R.id.item_cart_add:
                int count = goodsCart.getCount();
                count++;
                goodsCart.setCount(count);
                break;
            case R.id.item_cart_sub:
                count = goodsCart.getCount();
                if (count == 0){
                    count = 1;
                }
                count--;
                goodsCart.setCount(count);
                break;
        }
        adapter.notifyDataSetChanged();
        calcTotalFee();
    }

    //TODO 写一个方法进行总金额的计算
    public void calcTotalFee(){
        float totalFee = 0;
        int checkCount = 0;
        for (GoodsCart goodsCart : goodsCarts) {
            if (goodsCart.isChecked()){
                String productPrice = goodsCart.getGoods_price();
                productPrice = productPrice.replace("￥","");
                productPrice = productPrice.replace("元","");
                float price = Float.parseFloat(productPrice);
                int count = goodsCart.getCount();
                float p = price * count;// 计算一个条目选中的总金额
                totalFee += p;
                checkCount += count;
            }
        }

        txtTotalFee.setText("总金额 ： " + Float.toString(totalFee) + "元");
        if (checkCount > 0){
            btnCheckout.setText("去结算(" + checkCount + ")");
        }else {
            btnCheckout.setText("去结算");
        }
    }

    @Override
    public void processResult(JSONObject jsonObject) {
        try {
            int code = jsonObject.getInt("code");
            if (code == 1){
                JSONObject msg = jsonObject.getJSONObject("msg");
                JSONObject dataObject = msg.getJSONObject("data");
                JSONArray goods_list = dataObject.getJSONArray("goods_list");
                for (int i = 0; i < goods_list.length(); i++) {
                    JSONObject object = goods_list.getJSONObject(i);
                    GoodsCart goodsCart = new GoodsCart();
                    goodsCart.parseJSON(object);
                    goodsCarts.add(goodsCart);
                }
                adapter.notifyDataSetChanged();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}